
var totalPrice = getUrlParam("price");
$("#totalPrice").text(totalPrice);
var couponType = ["场地","场地布置","达人服务","包车服务","餐饮服务","套餐","所有"];
var orderInput = $("#ordersInput");
$.ajax({
  url:"/coup/list/use",
  data:{
    siteTotalAmount:parseFloat(getUrlParam("site")),
    talentServiceTotalAmount:parseFloat(getUrlParam("person")),
    caterTotalAmount:parseFloat(getUrlParam("catering")),
    busTotalAmount:parseFloat(getUrlParam("car")),
    setMealTotalAmount:parseFloat(getUrlParam("price")),
    decorationProductTotalAmount:parseFloat(getUrlParam("layout")),
  },
  type:"get",
  dataType:"json",
  success:function(res){
      var element = document.getElementById("couponList");
    if(res.result.length==0){
      var info = '<p style="height:60px;text-align:center;line-height:60px;">没有可用优惠劵</p>';
      element.innerHTML = info;
    }
    for(var i=0;i<res.result.length;i++){
      res.result[i].type=couponType[res.result[i].productType];
      res.result[i].startTime =  new Date(res.result[i].startTime).format('yyyy:MM:dd');
      res.result[i].endTime =  new Date(res.result[i].endTime).format('yyyy:MM:dd');
    }
    var allTpl = template("couponTpl",res);
    //console.log(document.getElementById(elementId)+"----");

    element.innerHTML = element.innerHTML+allTpl;
    $(".select-coupon").change(function(){
      var self = $(this);
      if(self.attr("name")=='6'){
        $(".select-coupon:not(input[name='6'])").attr("checked",false);
      }else{
        $(".select-coupon[name='6']").attr("checked",false);
      }

    });
  }
});

$("#comfirmCoupon").click(function(){
  var radio = $(".select-coupon:checked");
  var n=0,price=0;
  radio.each(function(){
      // console.log(this.value);
      var input = '<input name="cpids" type="hidden" value="'+this.value+'" />';
      orderInput.append(input);
      price +=parseFloat(this.getAttribute("data-price"));
      n++;
  });
  document.getElementById("checkCoupon").innerHTML = "已选择"+n+"张优惠劵";
  closeCoupon();
  console.log(totalPrice-price);
  $("#totalPrice").text(parseFloat(totalPrice-price).toFixed(2));
});


// console.log(ordersNum);

  $("#submit1").click(function() {
    if(is_weixn()){
      $("#orderPage").hide();
      $("#payPage").show();
      $("#couponPage").hide();
      _system._guide(true);
    }else{
      if($("input[name='activityTime']").val()=="选择时间"){
        mui.toast("请选择时间");
        return;
      }
      if($("input[name='contactUser']").val()==""){
        mui.toast("请填写联系姓名");
        return;
      }
      if($("input[name='contactPhone']").val()==""){
        mui.toast("请填写联系手机");
        return;
      }
      mui.ajax('/shop/add?productType=聚会套餐&productId='+getUrlParam("id")+'&admin.aid=' + getUrlParam("adminId"),{
        type: 'get',
        async: false,
        success: function(res) {
          if (res.code == 200) {
            var input = '<input name="spids" type="hidden" value="'+res.result+'" />';
            orderInput.append(input);
            $("form").submit();
          } else if (res.code == 500) {

          }
        },
      });


    }

  });

$("#datePicker").click(function() {
  //var optionsJson = '{"type":"hour","customData":{"h":[{"text":"上午","value":"上午"},{"text":"下午","value":"下午"},{"text":"晚上","value":"晚上"}]},"labels":["年", "月", "日", "时段", "分"]}';
  var optionsJson = '{"type":"date","beginYear":2017,"endYear":2030}';
  var dtpicker = new mui.DtPicker(JSON.parse(optionsJson))
  dtpicker.show(function(e) {
    $("#datePicker").val(e.text);
  });
});

$("#checkCoupon").click(function() {
  $("#orderPage").hide();
  $("#payPage").hide();
  $("#couponPage").show();
});

$("#closeCoupon").click(function() {
  closeCoupon();
});

function closeCoupon(){
  $("#orderPage").show();
  $("#payPage").hide();
  $("#couponPage").hide();
}

mui.init({
  pullRefresh: {
    container: '#pullrefresh',
    down: {
      auto:true,
      callback: function() {

      }
    },
    up: {
      contentrefresh: '正在加载...',
      callback: function() {

      }
    }
  }
});
